package org.algorithm.YaYa.链表;

import org.algorithm.biubiu.wheel_1.common.ListNode;
import org.junit.Test;

/**
 * @Date 2022/2/26
 * @Link {https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list-ii}
 */
public class 删除排序链表中的重复元素II {



    @Test
    public  void test(){

        ListNode listNode = deleteDuplicates(ListNode.build(1, 2, 3, 3, 4, 4, 5));
        System.out.println("listNode = " + listNode);

    }




    public ListNode deleteDuplicates(ListNode head) {
        ListNode pre = new ListNode(-1), tmp = pre;
        while (head != null) {
            ListNode t = head.next;
            int count = 1;
            while (t != null) {
                if (t.val == head.val) {
                    count++;
                    t = t.next;
                } else {
                    break;
                }
            }

            if (count == 1) {
                tmp.next = head;
                tmp = tmp.next;
                head = head.next;
            } else {
                head = t;
            }
        }
        if (tmp != null) tmp.next = null;
        return pre.next;
    }
}
